package com.bw.Threads.pattern_Lock.pattern;

import java.util.List;

public class Producer implements Runnable{
    private List<String> backet;
    private Integer maxSize;
    private LockObject lockObj ;

    public Producer(List<String> backet, Integer maxSize, LockObject lockObj ) {
        this.backet = backet;
        this.maxSize = maxSize;
        this.lockObj  = lockObj ;
    }

    @Override
    public void run() {
        while (true) {
            lockObj .lock.lock();//上锁
            while (maxSize == backet.size()){
                System.out.println("缓冲区已经满了");
                try {
                    lockObj.lockCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            backet.add("包子");
            System.out.println(Thread.currentThread().getName()+"  档期的包子个数"+backet.size());
            lockObj.lockCondition.signalAll();//唤醒其他线程
            lockObj.lock.unlock();//关闭本次线程
        }
    }
}
